#include "volumetool.h"
#include "ui_volumetool.h"
#include<QGraphicsDropShadowEffect>
#include<QPainter>
#include<QDebug>
VolumeTool::VolumeTool(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VolumeTool),_volume(20)
{
    ui->setupUi(this);
    ui->sliderBox->installEventFilter(this);
    //设置为弹射窗口；相对于全局坐标系了
    this->isMute = false;
    setWindowFlags(Qt::Popup|Qt::FramelessWindowHint|Qt::NoDropShadowWindowHint);
    //设置控件为透明状态；
   setAttribute(Qt::WA_TranslucentBackground);//有默认的底色为黑色；
   //设置边框阴影效果；
    QGraphicsDropShadowEffect*shdowEffect = new QGraphicsDropShadowEffect(this);
    shdowEffect->setColor("#646464");
    shdowEffect->setOffset(0,0);
    shdowEffect->setBlurRadius(10);
    this->setGraphicsEffect(shdowEffect);
    //设置音量初始化；
    QRect out = ui->outLine->geometry();
    ui->outLine->setGeometry(QRect(out.x(),out.y()+180 - 36,4,36));
    ui->silenceBt->setIcon(QIcon(":/image/volumn.png"));
    ui->volumeRadio->setText("%20");
    ui->sliderBt->move(ui->sliderBt->x(),ui->outLine->y()-ui->sliderBt->height()/2);
}

VolumeTool::~VolumeTool()
{
    delete ui;
}

void VolumeTool::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setPen(QPen(Qt::NoPen));//生成画笔，没有线条
    //生成画刷；
    painter.setBrush(QBrush(Qt::white));
    QPolygon polygon;
    QPoint a(ui->volumeBox->x()+20,ui->volumeBox->y()+300);
    QPoint b(ui->volumeBox->x() + 60,ui->volumeBox->y()+300);
    QPoint c(a.x() + 20,a.y() + 20);
    polygon.append(a);
    polygon.append(b);
    polygon.append(c);
    painter.drawPolygon(polygon);

    (void)event;

}

bool VolumeTool::eventFilter(QObject *object, QEvent *event)
{
    if(object == ui->sliderBox)
    {
        //在音量控制区域，捕获事件了；
        if(event->type() == QEvent::MouseButtonPress)
        {
            //按下去；计算音量大小
            setVolume();
        }
        else if(event->type() == QEvent::MouseButtonRelease)
        {
            //松开，发送信号；
            emit volumeChanged(_volume);
        }
        else if(event->type() == QEvent::MouseMove)
        {
            //计算大小，发送信号；
            setVolume();
            emit volumeChanged(_volume);
        }
        return true;
    }

    return QObject::eventFilter(object,event);
}

void VolumeTool::setVolume()
{
    //获取当前光标的坐标（屏幕）,y坐标；
    int heigth = ui->sliderBox->mapFromGlobal(QCursor().pos()).y();//heigth(25 - 205);
    if(heigth<=25)
    {
        heigth = 25;
    }
    if(heigth >= 205)
    {
        heigth = 205;
    }
    //计算outline的位置；
    ui->outLine->setGeometry(ui->outLine->x(),heigth,ui->outLine->width(),205 - heigth);
    //设置sliderbt位置
    ui->sliderBt->move(ui->sliderBt->x(),ui->outLine->y() - ui->sliderBt->height()/2);
    _volume = (ui->outLine->height() /(double)180)*100;
    ui->volumeRadio->setText(QString::number(_volume,10)+"%");

}

void VolumeTool::on_silenceBt_clicked()
{
    isMute = !isMute;
    if(isMute)
    {
        ui->silenceBt->setIcon(QIcon(":/image/silent.png"));
    }
    else
    {
        ui->silenceBt->setIcon(QIcon(":/image/volumn.png"));
    }
    emit muteChanged(isMute);
}
